package com.example.userservice.utils;

import java.util.*;

public class Client {

    public static void main(String[] args) {
//        print();
        int[] nums = {0, 1, 0, 3, 12};
//        game();
        moveZeroes(nums);

        System.out.println("xxx");
    }

    public static void print() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i * j + " ");
            }
            System.out.println("");
        }
    }


    public static void game() {
        Random random = new Random();
        int target = random.nextInt(180) + 1; // ① 随机生成一个 1-180 之间的数据

        Scanner scanner = new Scanner(System.in);
        while (true) { // 定义一个死循环让用户可以一直猜测
            System.out.print("请输入你猜测的数字（1-180）：");
            int guess = scanner.nextInt();

            if (guess < target) {
                System.out.println("过小"); // 猜小提示过小
            } else if (guess > target) {
                System.out.println("过大"); // 猜大提示过大
            } else {
                System.out.println("恭喜猜中！游戏结束。"); // 猜中则结束游戏
                break;
            }
        }

        scanner.close();
    }


    public List<Integer> findTarget(int[] nums, int target) {
        List<Integer> ans = new ArrayList<>();
        Map<Integer, Integer> map = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                ans.add(map.get(complement));
                ans.add(i);
                return ans;
            } else {
                map.put(nums[i], i);
            }
        }
        return ans;
    }


    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> ans = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        Integer index = 0;
        for(String s :strs) {
            String key = sortString(s);
            if(map.containsKey(key)) {
                ans.get(map.get(key)).add(s);
            } else {
                ans.add(new ArrayList<>());
                ans.get(index).add(s);
                map.put(key, index);
                index++;
            }
        }
        return ans;
    }

    public String sortString(String s) {
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }

    public int longestConsecutive(int[] nums) {
        int ans = 0;
        Set<Integer> set = new HashSet<>();
        for(int i: nums) {
            set.add(i);
        }
        for(int i: set) {
            if (!set.contains(i - 1)) {
                int count = 1;
                int target = i + 1;
                while (set.contains(target)) {
                    count++;
                    target++;
                }
                ans = Math.max(ans, count);
            }
        }
        return ans;
    }

    public static void moveZeroes(int[] nums) {
        int left = 0;
        int right = 0;
        while (right < nums.length) {
            if (nums[right] != 0) {
                swap(nums, left, right);
                left++;
                right++;
            } else {
                right++;
            }
        }
    }

    public static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public int subarraySum(int[] nums, int k) {
        return 0;
    }
}
